package com.anpy.bill.controller;

import com.anpy.common.service.BaseController;
import com.anpy.common.service.model.vo.ResponseMessage;
import com.anpy.common.service.model.vo.Result;
import com.anpy.bill.model.param.DateLineCreateDTO;
import com.anpy.bill.model.param.DateLineQueryDTO;
import com.anpy.bill.model.param.DateLineUpdateDTO;
import com.anpy.bill.service.DateLineService;
import com.anpy.bill.service.DateLineTypeService;
import com.anpy.bill.model.vo.DateLineVO;
import com.anpy.bill.entity.DateLineTypeEntity;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * 家庭日历控制器
 */
@RestController
@RequestMapping("/api/dateline")
@Api(tags = "家庭日历接口")
@ApiImplicitParams({
   @ApiImplicitParam(paramType = "header", name = "authorization", value = "操作人手机号码", dataType = "String", required = true, example = "185xxxxx"),
})
public class DateLineController extends BaseController {

    @Resource
    private DateLineService dateLineService;

    @Resource
    private DateLineTypeService dateLineTypeService;

    public DateLineController(HttpServletRequest request) {
        super(request);
    }

    /**
     * 创建日历事件
     */
    @PostMapping("/create")
    @ApiOperation("创建日历事件")
    public ResponseMessage<Boolean> createDateLine(@RequestBody @Valid @ApiParam("日历事件信息") DateLineCreateDTO createDTO) {
        Integer userId = getUserId().intValue();
        return Result.success(dateLineService.createDateLine(createDTO, userId));
    }

    /**
     * 更新日历事件
     */
    @PostMapping("/update")
    @ApiOperation("更新日历事件")
    public ResponseMessage<Boolean> updateDateLine(@RequestBody @Valid @ApiParam("日历事件信息") DateLineUpdateDTO updateDTO) {
        Integer userId = getUserId().intValue();
        return Result.success(dateLineService.updateDateLine(updateDTO, userId));
    }

    /**
     * 删除日历事件
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除日历事件")
    public ResponseMessage<Boolean> deleteDateLine(@PathVariable("id") @ApiParam("事件ID") Long id) {
        Integer userId = getUserId().intValue();
        return Result.success(dateLineService.deleteDateLine(id, userId));
    }

    /**
     * 获取日历事件详情
     */
    @GetMapping("/get/{id}")
    @ApiOperation("获取日历事件详情")
    public ResponseMessage<DateLineVO> getDateLineDetail(@PathVariable("id") @ApiParam("事件ID") Long id) {
        Integer userId = getUserId().intValue();
        DateLineVO dateLineVO = dateLineService.getDateLineDetail(id, userId);
        return Result.success(dateLineVO);
    }

    /**
     * 分页查询日历事件
     */
    @PostMapping("/query")
    @ApiOperation("分页查询日历事件")
    public ResponseMessage<IPage<DateLineVO>> queryDateLines(@RequestBody @ApiParam("查询条件") DateLineQueryDTO queryDTO) {
        Integer userId = getUserId().intValue();
        IPage<DateLineVO> result = dateLineService.queryDateLines(queryDTO, userId);
        return Result.success(result);
    }

    /**
     * 根据时间和事件类型查询（GET方式）
     */
    @GetMapping("/list")
    @ApiOperation("根据条件查询日历事件")
    public ResponseMessage<IPage<DateLineVO>> getDateLinesByCondition(
            @RequestParam(required = false) @ApiParam("事件类型ID") Long eventTypeId,
            @RequestParam(required = false) @ApiParam("开始时间(yyyy-MM-dd)") String startTime,
            @RequestParam(required = false) @ApiParam("结束时间(yyyy-MM-dd)") String endTime,
            @RequestParam(defaultValue = "1") @ApiParam("页码") Integer pageNum,
            @RequestParam(defaultValue = "10") @ApiParam("每页大小") Integer pageSize) {
        
        DateLineQueryDTO queryDTO = new DateLineQueryDTO();
        queryDTO.setEventTypeId(eventTypeId);
        queryDTO.setPageNum(pageNum);
        queryDTO.setPageSize(pageSize);
        
        // 处理时间参数
        try {
            if (startTime != null && !startTime.isEmpty()) {
                queryDTO.setStartTime(java.sql.Date.valueOf(startTime));
            }
            if (endTime != null && !endTime.isEmpty()) {
                queryDTO.setEndTime(java.sql.Date.valueOf(endTime));
            }
        } catch (IllegalArgumentException e) {
            return Result.error("时间格式错误，请使用yyyy-MM-dd格式");
        }
        
        Integer userId = getUserId().intValue();
        IPage<DateLineVO> result = dateLineService.queryDateLines(queryDTO, userId);
        return Result.success(result);
    }

    // ==================== 日历类型管理接口 ====================

    /**
     * 获取所有日历事件类型
     */
    @GetMapping("/type/list")
    @ApiOperation("获取所有日历事件类型")
    public ResponseMessage<List<DateLineTypeEntity>> getAllDateLineTypes() {
        Integer userId = getUserId().intValue();
        List<DateLineTypeEntity> types = dateLineTypeService.getDateLineTypesByUserId(userId);
        return Result.success(types);
    }

    /**
     * 根据ID获取日历事件类型详情
     */
    @GetMapping("/type/get/{id}")
    @ApiOperation("获取日历事件类型详情")
    public ResponseMessage<DateLineTypeEntity> getDateLineTypeDetail(@PathVariable("id") @ApiParam("类型ID") Long id) {
        Integer userId = getUserId().intValue();
        DateLineTypeEntity type = dateLineTypeService.getDateLineTypeDetail(id, userId);
        return Result.success(type);
    }

    /**
     * 创建日历事件类型
     */
    @PostMapping("/type/create")
    @ApiOperation("创建日历事件类型")
    public ResponseMessage<Boolean> createDateLineType(@RequestBody @Valid @ApiParam("类型信息") DateLineTypeEntity typeEntity) {
        Integer userId = getUserId().intValue();
        typeEntity.setUserId(userId);
        return Result.success(dateLineTypeService.createDateLineType(typeEntity));
    }

    /**
     * 更新日历事件类型
     */
    @PostMapping("/type/update")
    @ApiOperation("更新日历事件类型")
    public ResponseMessage<Boolean> updateDateLineType(@RequestBody @Valid @ApiParam("类型信息") DateLineTypeEntity typeEntity) {
        Integer userId = getUserId().intValue();
        return Result.success(dateLineTypeService.updateDateLineType(typeEntity, userId));
    }

    /**
     * 删除日历事件类型
     */
    @DeleteMapping("/type/delete/{id}")
    @ApiOperation("删除日历事件类型")
    public ResponseMessage<Boolean> deleteeDateLineType(@PathVariable("id") @ApiParam("类型ID") Long id) {
        Integer userId = getUserId().intValue();
        return Result.success(dateLineTypeService.deleteeDateLineType(id, userId));
    }
}
